code (Python)
from collections import deque
# The following code creates an array of numbers starting at 0 and smoothly increasing to 257
n = 3
# creates an array of 0's of size 2^n + 1
x = [0]*(2**n + 1)
# set the value for the last element
x[len(x) - 1] = 257
# we create a queue of the line segments we need to subdivide
q = deque()
# and add the first segment to it
q.append((0, len(x) - 1))
# now we go through and subdivide each segment
while len(q) != 0:
left, right = q.popleft()
midpoint = (left + right) // 2
# set the midpoint to the average of the two ends
x[midpoint] = (x[left] + x[right]) // 2
# if the width of the segment is greater than 2 then it can be subdivided
if right - left > 2:
q.append((left, midpoint))
q.append((midpoint, right))
print(x) # prints: [0, 32, 64, 96, 128, 160, 192, 224, 257]